﻿package com.kuanter.kuanterauto.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.umeng.analytics.MobclickAgent;

public class PersonalInfoActivity extends WuyangBaseActivity {

	private static final int PLEASE_AUTOCODE_PHONE = 0;
	private static final int PLEASE_AUTOCODE_EMAIL = 1;
	private Intent intent = new Intent();
	private Button personal_info_header_left_btn;
	private View modify_phone_number_goto;
	private View modify_email_goto;
	private View modify_password_goto;
	private String userId;
	private String userEmail;
	private String mobileNumber;
	private String userRealName;
	private TextView userRealName_tv;
	private TextView mobileNumber_tv;
	private TextView userEmail_tv;
	private TextView userAddress_tv;
	private String address;
	/*@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		this.setBodyControlViewText();
		super.onResume();
	}*/
	public void onResume() {
	    super.onResume();
	    MobclickAgent.onResume(this);
	}
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(this);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.personal_info);
		this.initBodyControlView();
		this.registerListener();
		this.setBodyControlViewText();
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(null != data){
			switch (requestCode) {
			case PersonalInfoActivity.PLEASE_AUTOCODE_PHONE:
				mobileNumber_tv.setText(data.getStringExtra("phone_nubmer"));
				SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
				Editor editor = sp.edit();
				editor.putString("mobileNumber", data.getStringExtra("phone_nubmer"));
				editor.commit();
				this.setBodyControlViewText();
				break;

			case PersonalInfoActivity.PLEASE_AUTOCODE_EMAIL:
				userEmail_tv.setText(data.getStringExtra("email"));
				SharedPreferences sp1 = getSharedPreferences("userinfo", MODE_PRIVATE);
				Editor editor1 = sp1.edit();
				editor1.putString("userEmail", data.getStringExtra("email"));
				editor1.commit();
				this.setBodyControlViewText();
				break;
			}
		}
		
		super.onActivityResult(requestCode, resultCode, data);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.personal_info_header_left_btn:
			this.finish();
			break;
		case R.id.modify_phone_number_goto:
			intent.setClass(this, PersonalInfoPhoneNumberSettingActivity.class);
			startActivityForResult(intent, PersonalInfoActivity.PLEASE_AUTOCODE_PHONE) ;
			break;
		case R.id.modify_email_goto:
			intent.setClass(this, PersonalInfoEmailSettingActivity.class);
			startActivityForResult(intent, PersonalInfoActivity.PLEASE_AUTOCODE_EMAIL) ;
			break;
		case R.id.modify_password_goto:
			intent.setClass(this, PersonalInfoPasswordSettingActivity.class);
			startActivity(intent) ;
			break;
		default:
			break;
		}
	}

	@Override
	protected void setHeaderControlViewText() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void initBodyControlView() {
		// TODO Auto-generated method stub
		personal_info_header_left_btn = (Button)this.findViewById(R.id.personal_info_header_left_btn);
		modify_phone_number_goto = this.findViewById(R.id.modify_phone_number_goto);
		modify_email_goto = this.findViewById(R.id.modify_email_goto);
		modify_password_goto = this.findViewById(R.id.modify_password_goto);
		//用户名
		userRealName_tv = (TextView)this.findViewById(R.id.userRealName_tv);
		//手机号码
		mobileNumber_tv = (TextView)this.findViewById(R.id.mobileNumber_tv);
		//email
		userEmail_tv = (TextView)this.findViewById(R.id.userEmail_tv);
		//user address
		userAddress_tv = (TextView)this.findViewById(R.id.userAddress_tv);
	}

	@Override
	protected void registerListener() {
		// TODO Auto-generated method stub
		personal_info_header_left_btn.setOnClickListener(this);
		modify_phone_number_goto.setOnClickListener(this);
		modify_email_goto.setOnClickListener(this);
		modify_password_goto.setOnClickListener(this);
	}

	@Override
	protected void setBodyControlViewText() {
		// TODO Auto-generated method stub
		
		SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
		userId = sp.getString("id", "");
		mobileNumber = sp.getString("mobileNumber", "");
		userEmail = sp.getString("userEmail", "");
		userRealName = sp.getString("userRealName", "");
		address = sp.getString("address", "");
		//给控件附个人信息值
		userRealName_tv.setText(userRealName);
		mobileNumber_tv.setText(mobileNumber);
		userEmail_tv.setText(userEmail);
		if(null != address&&!"".equals(address)){
			userAddress_tv.setText(address);
		}else{
			userAddress_tv.setText("暂无");
		}
	}

	@Override
	protected void getControlViewText() {
		// TODO Auto-generated method stub
		
	}
	
}
